package jvm.jtacck.db.records;

/**
 * 
 * @author Chris Gamble
 * 
 *         Represents a relational table of a database. Shipping information
 *         relates a Specimen to other records. the username who started the
 *         tracking process (shipped), the client_site that it is destined for,
 *         the status of the specimen, and the username of the courier who has
 *         picked up the specimen if applicable.
 */
public class ShippingInformation extends Record {

    private static final long serialVersionUID = -1632753780672299640L;
    public final Specimen specimen;
    public final User shipper;
    public final ClientSite destination;
    public final Courier courier;
    public final Status status;

    /**
     * Constructs a ShippingInformation relation
     * 
     * @param spec
     *            the Specimen the relation refers to, used as unique idd
     *            NON-NULLABLE
     * @param shipper
     *            the User who shipped the Specimen
     *            NON-NULLABLE
     * @param dest
     *            the ClientSite that is the final destination of the Specimen
     *            NON-NULLABLE
     * @param cour
     *            the Courier who is transporting the Specimen
     * @param stat
     *            the Status of the Specimen
     *            NON-NULLABLE
     */
    public ShippingInformation(Specimen spec, User shipper, ClientSite dest,
            Status stat, Courier cour) {
        this.specimen = spec;
        this.shipper = shipper;
        this.destination = dest;
        this.courier = cour;
        this.status = stat;
    }
    
    @Override
    public boolean equals(Object o){
        if (o instanceof ShippingInformation) {
            ShippingInformation other = (ShippingInformation) o;
            boolean x = 
                specimen.equals(other.specimen) &&
                   shipper.equals(other.shipper) &&
                   destination.equals(other.destination) &&
                   checkCourier(courier, other.courier) &&
                   status.equals(other.status);
            return x;
        }
        return false;
    }
    
    public boolean checkCourier(Courier s1, Courier s2) {
        if (s1 == null) {
            return s2 == null;
        }else {
            return s1.equals(s2);
        }
    }
    @Override
    public int hashCode() {
        return specimen.hashCode() + shipper.hashCode() + 
        destination.hashCode() + status.hashCode();
    }
    
    public String toString() {
        return specimen.barcode + ": " + shipper.username + ", " +  
                destination.name + ", " + status.name + ", " + courier + ".";
    }
}
